package com.derbysoft.lowcostadapter.schedule.sync.impl;

import com.derbysoft.lowcostadapter.commons.redis.service.JedisClusterService;
import com.derbysoft.lowcostadapter.schedule.sync.SyncService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.atomic.AtomicBoolean;

public abstract class SyncServiceBase implements SyncService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    protected AtomicBoolean running = new AtomicBoolean(false);

    @Autowired
    protected JedisClusterService redisCommands;

    @Override
    public synchronized void sync() {
        try {
            running.set(true);

            syncInternal();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally {
            running.set(false);
        }
    }

    @Override
    public boolean isRunning() {
        return running.get();
    }

    protected abstract void syncInternal();
}
